package com.huayi.website.controller;

import com.huayi.website.entity.EnumLanguage;
import com.huayi.website.entity.EnumPages;
import com.huayi.website.service.BannerService;
import com.huayi.website.service.MessageBoardService;
import com.huayi.website.service.PageContentService;
import com.huayi.website.service.PagesService;
import com.huayi.website.utils.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class ContactController {
    @Autowired
    private BannerService bannerService;
    @Autowired
    private PageContentService pageContentService;
    @Autowired
    private PagesService pagesService;
    @Autowired
    private MessageBoardService messageBoardService;

    @RequestMapping("/{language}/contact")
    public String home(HttpServletRequest request, @PathVariable String language, Model model){
        System.out.println(language);
        EnumLanguage languageEnum = EnumLanguage.getEnum(language);

        var banners = bannerService.getAllList(EnumPages.LinkUs);
        model.addAttribute("banners",banners);
        var pageContent = pageContentService.getByPageIdAndLanguage(EnumPages.LinkUs.getIndex(), languageEnum);
        model.addAttribute("pc",pageContent);
        model.addAttribute("menuId","contact");

        //获取页面信息，设置title
        var page = pagesService.getById(EnumPages.LinkUs);
        if (languageEnum == EnumLanguage.CHINESE) {
            model.addAttribute("layout", "layout_zh");
            model.addAttribute("pageTitle", page.getTitleCn());
        }
        else {
            model.addAttribute("pageTitle", page.getTitleEn());
            model.addAttribute("layout", "layout");
        }

        return "contact";
    }

    @ResponseBody
    @RequestMapping(value = "/message_submit", method = RequestMethod.POST)
    public ResultVo<String> publishPost(Model model, HttpServletRequest request) {

        String userName = request.getParameter("userName");
        String phone = request.getParameter("phone");
        String email = request.getParameter("email");
        String content = request.getParameter("content");

        messageBoardService.addMessage(userName,phone,email,content);
        return new ResultVo<>(0, "SUCCESS");
    }
}
